import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Users from '@/components/Users'
import Rights from '@/components/power/Rights'
import Roles from '@/components/power/Roles'
import GoodsCate from '@/components/goodsAdmin/GoodsCate'
import GoodsList from '@/components/goodsAdmin/GoodsList'
import CateParams from '@/components/goodsAdmin/CateParams'
import Add from '@/components/goodsAdmin/Add'
import Order from '@/components/order/Order'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', name: 'Welcome', component: Welcome },
        { path: '/users', name: 'Users', component: Users },
        { path: '/rights', name: 'Rights', component: Rights },
        { path: '/roles', name: 'Roles', component: Roles },
        { path: '/categories', name: 'GoodsCate', component: GoodsCate },
        { path: '/goods', name: 'GoodsList', component: GoodsList },
        { path: '/params', name: 'CateParams', component: CateParams },
        { path: '/good/add', name: 'Add', component: Add },
        { path: '/orders', name: 'Order', component: Order }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
